<template>

  <div>

    <el-table :data="tableData" header-cell-class-name="table-header" size="medium" border @row-click="rowClick">
      <el-table-column type="tags" label="终端名称" fixed="left" align="center" prop="terminalName" width="150" show-overflow-tooltip />
      <el-table-column type="tags" label="终端所在地" fixed="left" align="left" header-align="center" width="150" show-overflow-tooltip>
        <template #default="{row}">
          {{ row.provinceName }}/{{ row.cityName }}/{{ row.areaName }}
        </template>
      </el-table-column>
      <el-table-column type="tags" label="产品通用名" fixed="left" align="left" header-align="center" prop="productPopularName" width="150" show-overflow-tooltip />
      <el-table-column type="tags" label="商品名" align="center" prop="productName" width="100" show-overflow-tooltip />
      <el-table-column type="tags" label="包装规格" align="center" prop="packSpecs" width="100" show-overflow-tooltip />
      <el-table-column type="tags" label="终端价(元)" align="center" prop="terminalPrice" />
      <el-table-column type="tags" label="交易价(元)" align="center" prop="tradePrice" />
      <el-table-column type="tags" label="配送商" align="center" width="200">
        <template #default="{row}">
          <distributors-view
            v-if="row.distributeContractDetails && row.distributeContractDetails.length"
            :datas="row.distributeContractDetails"
            :unit="row.sellUnit"
          />
        </template>
      </el-table-column>
      <el-table-column type="tags" label="交易状态" align="center" width="100">
        <template #default="{row}">
          {{ getStatus(row) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center" min-width="100px">
        <template #default="{row}">
          <el-button v-if="row.devStatus > 1" type="text" @click="handlSet(row)">设置配送信息</el-button>
          <el-button v-if="row.devStatus == -1 || row.devStatus == -6" type="text" class="delete-text-btn" @click.stop="onDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <Pagination
      :page="formData.page"
      :limit="formData.pageSize"
      style="display: flex; justify-content: flex-end"
      :total="total"
      @pagination="getList"
    />

  </div>

  <!-- 产品终端价详情 -->
  <detail-drawer
    ref="detailDrawer"
    :size-width="1000"
    custom-title
  >
    <template #title>
      <span>区域招商配送终端 - {{ select.priceNumber }}</span>
      <el-tag type="warning" size="small" style="padding-left: 10px">{{ getStatus(select) }}</el-tag>
    </template>
    <template #contents>

      <terAndDisDetail :id="select.devId" />

    </template>
  </detail-drawer>

  <!--设置配送信息-->
  <setiDelivery ref="setiDeliveryDialog" @distributeSuccsess="distributeSuccsess" />
</template>

<script>
import {useRoute} from 'vue-router'
import {defineComponent, defineAsyncComponent, getCurrentInstance, inject, onMounted, reactive, toRefs} from 'vue'

export default defineComponent({
  name: 'TerAndDisView',
  components: {
    terAndDisDetail:defineAsyncComponent(
            ()=>import('./terAndDisDetail.vue')
    ),
    setiDelivery :defineAsyncComponent(
            ()=>import('@CRM/views/marketing/customer/terminal/modules/setiDelivery.vue')
    )
  },
  setup() {
    const currentInstance = getCurrentInstance()
    const route = useRoute()
    const api = inject('api')
    var data = reactive({
      tableData: [],
      formData: {
        page: 1,
        pageSize: 20,
      },
      total: 0,
      select: {},
      detailDrawer: null,
      setiDeliveryDialog: null,
    })

    const getList = () => {
      const a = { ...data.formData }
      a.agentProductId = Number(route.query.id)
      api.signedAgent.DISTRIBUTE_CONTRACT_DETAIL_LIST(a).then(res => {
        if (!res.code) {
          data.tableData = res.data.list
        } else {
          currentInstance.proxy.customMessage({ type: 'error', message: res.msg })
        }
      })
    }

    const rowClick = (row, clu) => {
      if (clu.label == '操作') return
      data.select = row
      data.detailDrawer.open()
    }

    // 设置配送信息
    const handlSet = (val) => {
      api.terminal.PRODUCT_DETAILS(val.id).then((res) => {
        if (res.code === 0) {
          data.setiDeliveryDialog.open(res.data, val.id)
        }
      })
    }

    const onDelete = (row) => {
      currentInstance.proxy
        .alertBox({
          message: '是否确认删除此配送终端？',
          title: '删除',
        })
        .then(() => {
          api.supplyChannels.SUPPLY_CHANNELS_DELETE(row.devId).then((res) => {
            if (res.code === 0) {
              getList()
            } else {
              currentInstance.proxy.customMessage({ type: 'error', message: res.msg })
            }
          })
        })
        .catch((err) => {
          console.log(err)
        })
    }

    const getStatus = (row) => {
      let str = ''
      switch (row.devStatus) {
        case '-6':
          str = '审核未通过'
          break
        case '-7':
          str = '审核中'
          break
        case '-3':
          str = '终止交易'
          break
        case '-2':
          str = '开发失败'
          break
        case '-1':
          str = '审核未通过'
          break
        case '0':
          str = ' 审核中'
          break
        case '1':
          str = '待开发'
          break
        case '2':
          str = '开发中'
          break
        case '4':
          str = '待进货'
          break
        case '5':
          str = '开发成功'
          break
        case '7':
          str = '正常交易'
          break
      }
      return str
    }

    onMounted(() => {
      getList()
    })

    return {
      ...toRefs(data),

      getList,
      rowClick,
      getStatus,
      handlSet,
      onDelete,
    }
  }
})
</script>

<style scoped>

</style>
